//
//  KZWrongCountView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/10/19.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZWrongCountView.h"

@implementation KZWrongCountView
{
    CALayer *_ringLayer1;
    CALayer *_ringLayer2;
    
    UILabel *_countLabel;
}

- (instancetype)init {
    if (self = [super init]) {
        
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    self.backgroundColor = UIColor.whiteColor;
    self.alpha = 0.8;
    self.layer.cornerRadius = W(50);
    self.layer.masksToBounds = false;
    
    [self layoutIfNeeded];
    
    _ringLayer1 = CALayer.new;
    _ringLayer1.opacity = 0.6;
    _ringLayer1.bounds = CGRectMake(0, 0, W(120), W(120));
    _ringLayer1.position = CGPointMake(W(50), W(50));
    _ringLayer1.cornerRadius = W(60);
    _ringLayer1.backgroundColor = UIColor.whiteColor.CGColor;
    [self.layer insertSublayer:_ringLayer1 atIndex:0];

    _ringLayer2 = CALayer.new;
    _ringLayer2.opacity = 0.3;
    _ringLayer2.bounds = CGRectMake(0, 0, W(140), W(140));
    _ringLayer2.position = CGPointMake(W(50), W(50));
    _ringLayer2.cornerRadius = W(70);
    _ringLayer2.backgroundColor = UIColor.whiteColor.CGColor;
    [self.layer insertSublayer:_ringLayer2 atIndex:0];

    
    _countLabel = [[UILabel alloc] init];
    _countLabel.adjustsFontSizeToFitWidth = true;
    _countLabel.numberOfLines = 1;
    _countLabel.text = @"0";
    _countLabel.font = FONT(W(36));
    _countLabel.textAlignment = NSTextAlignmentCenter;
    _countLabel.textColor = RGBA(66, 152, 247, 1);
    [self addSubview:_countLabel];
    [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self).offset(-5);
        make.centerX.equalTo(self);
        make.left.right.equalTo(self);
    }];
    
    UILabel *label = [[UILabel alloc] init];
    label.adjustsFontSizeToFitWidth = true;
    label.numberOfLines = 1;
    label.text = @"错题总数量";
    label.font = FONT(W(12));
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor colorWithWhite:0.3 alpha:1];
    [self addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_countLabel.mas_bottom);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
    }];
}


- (void)_addAni:(CALayer *)layer {
    
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform"];
    
    ani.duration = 2;
    ani.repeatCount = HUGE;
    ani.fillMode = kCAFillModeForwards;
    ani.autoreverses = true;
    ani.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    ani.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.05, 1.05, 1)];
    
    [layer addAnimation:ani forKey:@"ani"];
}
- (void)addAnimation {
    [self _addAni:_ringLayer1];
    [self _addAni:_ringLayer2];
}
- (void)removeAnimation {
    [_ringLayer1 removeAllAnimations];
    [_ringLayer2 removeAllAnimations];
}

- (void)setCount:(NSInteger)count {
    _count = count;
    _countLabel.text = [NSString stringWithFormat:@"%li", count];
}


@end
